package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.Servicioadicional;
import services.ServicioAdicionalService;

@FacesConverter("servicioadicionalConverter")
public class ServicioAdicionalConverter implements Converter {
	
	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
        	ServicioAdicionalService service = (ServicioAdicionalService) fc.getExternalContext().getApplicationMap().get("servicioadicionalService");
            Iterator<Servicioadicional> i=service.getListaServicioAdicionales().iterator();

            while (i.hasNext()) {

            	Servicioadicional sa = (Servicioadicional) i.next();
				if(sa.getIdServicio() == Integer.parseInt(value))
					return sa;
			}
            return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
        	return String.valueOf(((Servicioadicional) object).getIdServicio());
        }
        else {
            return null;
        }
    }  

}
